package content 
{
	import net.WebClient;
	/**
	 * ...
	 * @author Ivan
	 */
	public class ContentManager 
	{
	
		private var _content:Content;
		private var _type:String;
		
		public function ContentManager(path:String, type:String = null) 
		{
			_content = new Content(path);
			_type = type;
		}
		
		public function update(list:Array):void 
		{
			//删除曾经下载过但是现在不需要的资源
			//节省内存但是增加CPU，建议超大地图才开启
			_content.each(delUnused, list);
			
			var currentLoadKey:*;
			
			var rb:Boolean = !(_content.isLoading && list.indexOf(_content.currentLoadingKey) > -1);
			_content.clear(rb);
			
			if (!rb)
			{
				currentLoadKey = _content.currentLoadingKey;
				trace(currentLoadKey + "rb false");
			}
			
			for (var i:int = 0, len:int = list.length; i < len; i++ )
			{
				if (list[i] != currentLoadKey && !_content.contails(list[i]))
					_content.load(list[i], WebClient.IMAGE, list[i] + _type)
			}
		}
		
		/**
		 * 删除未使用的资源
		 * @param	key
		 * @param	useList
		 */
		private function delUnused(key:Number, useList:Array):void 
		{
			if (useList.indexOf(key) == -1)
				_content.deleteContent(key);
		}
		
		
		/**
		 * 获取资源
		 * @param	key
		 * @return
		 */
		public function getContent(key:*):* 
		{
			
			return _content.getContent(key);
		}
		
	}

}